package com.cdqidi.core.weixinmanager.controller.api;

import com.cdqidi.core.weixinmanager.service.SubscribeService;
import com.cdqidi.core.weixinmanager.service.TemplateMsgReportService;
import com.cdqidi.core.weixinmanager.service.wx.WxMsgService;
import com.jfinal.springboot.weixin.annotation.ApiType;
import com.jfinal.springboot.weixin.annotation.WxApi;
import com.jfinal.springboot.weixin.jfinal.WeiXinMsgControllerAdapter;
import com.jfinal.weixin.sdk.msg.in.*;
import com.jfinal.weixin.sdk.msg.in.event.*;
import com.jfinal.weixin.sdk.msg.in.speech_recognition.InSpeechRecognitionResults;
import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * APP_微信推送公众号消息
 *
 * @author sjk
 * @create 2019-06-04 16:09
 * @since 1.0.0
 */
@Controller
@Slf4j
@WxApi(type = ApiType.WX)
@RequestMapping("/wxmanager/msg")
@RequiredArgsConstructor
public class WxMsgController extends WeiXinMsgControllerAdapter {
    private final WxMsgService wxMsgService;
    private final SubscribeService subscribeService;
    private final TemplateMsgReportService templateMsgReportService;

    @Override
    protected void processInTextMsg(InTextMsg inTextMsg) {
    }

    @Override
    protected void processInImageMsg(InImageMsg inImageMsg) {
    }

    @Override
    protected void processInVoiceMsg(InVoiceMsg inVoiceMsg) {
    }

    @Override
    protected void processInVideoMsg(InVideoMsg inVideoMsg) {
    }

    @Override
    protected void processInLocationMsg(InLocationMsg inLocationMsg) {
    }

    @Override
    protected void processInLinkMsg(InLinkMsg inLinkMsg) {
    }

    @Override
    protected void processInTemplateMsgEvent(InTemplateMsgEvent inTemplateMsgEvent) {
//        wxTemplateMsgService.saveTemplateMsgReport(inTemplateMsgEvent,ApiConfigKit.getAppId());
    }

    @Override
    protected void processInFollowEvent(InFollowEvent inFollowEvent) {
        OutTextMsg outTextMsg = wxMsgService.saveWxUserInfo(inFollowEvent);
        render(outTextMsg);
//        if(inFollowEvent.getEvent().equals(WxMsgService.SUBSCRIBE)){
        //如果是关注，那么发送模板消息，如果是取消关注则不处理
//            wxTemplateMsgService.sendMsg(inFollowEvent.getFromUserName(), ApiConfigKit.getAppId());
//        }
    }

    @Override
    protected void processInSubscribeNoticesEvent(InSubscribeNoticesEvent inSubscribeNoticesEvent) {
        List<?> list = inSubscribeNoticesEvent.getList();
        if (null == list || list.isEmpty()) {
            return;
        }
        if (inSubscribeNoticesEvent.isEventByMsgSent()) {
            //模板消息推送状态
            templateMsgReportService.saveMsg(inSubscribeNoticesEvent.getFromUserName(), inSubscribeNoticesEvent.getCreateTime(), inSubscribeNoticesEvent.getList());
        } else {
            subscribeService.saveOrUpdate(inSubscribeNoticesEvent.getFromUserName(), inSubscribeNoticesEvent.getCreateTime(), inSubscribeNoticesEvent.getList());
        }
    }


    @Override
    protected void processInQrCodeEvent(InQrCodeEvent inQrCodeEvent) {
    }

    @Override
    protected void processInLocationEvent(InLocationEvent inLocationEvent) {
    }

    @Override
    protected void processInMenuEvent(InMenuEvent inMenuEvent) {
    }

    @Override
    protected void processInSpeechRecognitionResults(InSpeechRecognitionResults inSpeechRecognitionResults) {
    }
}